import BootstrapDialog from 'bootstrap3-dialog';

$.dialogShow = function dialogShow(param) {
    var dialog = BootstrapDialog.show({
        title: param.title,
        size: param.size,
        closable: true,
        closeByBackdrop: false,
        closeByKeyboard: false,
        animate: true,
        message: param.message,
        draggable: true,
        onshow: function(dialog) {
            if (param.onshow) {
                param.onshow(dialog)
            }
        },
        onshown: function(dialog) {
            if (param.onshown) {
                param.onshown(dialog)
            }
        },
        onhide: function(dialog) {
            if (param.onhide) {
                param.onhide(dialog)
            }
        },
        onhidden: function(dialog) {
            if (param.onhidden) {
                param.onhidden(dialog)
            }
        }
    });
    return dialog;
}

$.msg = function msg(param) {
    BootstrapDialog.show({
        title: param.title,
        message: param.message,
        onshown: function(dialog) {
            setTimeout(function() {
                dialog.close();
            }, 1000);
        },
    });
}

$.dialog = function dialog(param) {
    // 创建方式1
    var dialog = BootstrapDialog.show({
        title: param.title,
        // [BootstrapDialog.TYPE_INFO, BootstrapDialog.TYPE_PRIMARY,  BootstrapDialog.TYPE_SUCCESS, BootstrapDialog.TYPE_WARNING, BootstrapDialog.TYPE_DANGER]
        type: param.type, // 默认 TYPE_INFO
        // [BootstrapDialog.SIZE_NORMAL, BootstrapDialog.SIZE_SMALL, BootstrapDialog.SIZE_WIDE, BootstrapDialog.SIZE_LARGE]
        size: param.size, // 默认 SIZE_NORMAL
        closable: true, // 关闭方式
        closeByBackdrop: false, // 关闭方式
        closeByKeyboard: false, // 关闭方式 键盘ecs
        animate: true, // 动画
        message: param.message,
        draggable: true, // 是否可拖动
        buttons: [{
            label: "确定",
            title: "确定",
            cssClass: "btn-primary btn-sm",
            // hotkey: 66, //热键 66 -> A | 13 -> Enter
            icon: "glyphicon glyphicon-plus",
            data: {},
            action: function(dialog) {
                param.submit(dialog);
            }
        }],
        onshow: function(dialog) {
            if (param.onshow) {
                param.onshow(dialog)
            }
        },
        onshown: function(dialog) {
            if (param.onshown) {
                param.onshown(dialog)
            }
        },
        onhide: function(dialog) {
            if (param.onhide) {
                param.onhide(dialog)
            }
        },
        onhidden: function(dialog) {
            if (param.onhidden) {
                param.onhidden(dialog)
            }
        }
    });
    return dialog;
}

$.confirm = function confirm(param) {
    var dialog = BootstrapDialog.confirm({
        title: param.title,
        message: param.message,
        type: BootstrapDialog.TYPE_WARNING,
        draggable: true,
        btnCancelLabel: '取消',
        btnOKLabel: '确定',
        btnOKClass: 'btn-warning',
        size: BootstrapDialog.SIZE_SMALL,
        callback: function(result) {
            if (result) {
                param.confirm();
            }
        }
    });
    return dialog;
}

// var dialog = new BootstrapDialog()
// .setTitle()
// .setMessage();
// BootstrapDialog.alert('Hi Apple!');
// BootstrapDialog.alert('Hi Apple!', function() {
//   alert('Hi Orange!');
// });
// BootstrapDialog.confirm('Hi Apple, are you sure?', function(result) {
//   if (result) {
//     alert('Yup.');
//   } else {
//     alert('Nope.');
//   }
// });
// BootstrapDialog.confirm({
//   title: 'WARNING',
//   message: 'Warning! Drop your banana?',
//   type: BootstrapDialog.TYPE_WARNING, // <-- Default value is BootstrapDialog.TYPE_PRIMARY
//   closable: true, // <-- Default value is false
//   draggable: true, // <-- Default value is false
//   btnCancelLabel: 'Do not drop it!', // <-- Default value is 'Cancel',
//   btnOKLabel: 'Drop it!', // <-- Default value is 'OK',
//   btnOKClass: 'btn-warning', // <-- If you didn't specify it, dialog type will be used,
//   callback: function(result) {
//     // result will be true if button was click, while it will be false if users close the dialog directly.
//     if (result) {
//       alert('Yup.');
//     } else {
//       alert('Nope.');
//     }
//   }
// });

// message: html
// message: "<div>this is dialog message</div>"
// message: $('<div></div>').load('remote.html')
// message: function(dialog) {
//     var $message = $('<div></div>');
//     var pageToLoad = dialog.getData('pageToLoad');
//     $message.load(pageToLoad);
//
//     return $message;
//   },
//   data: {
//     'pageToLoad': 'remote.html'
//   }
// message: function(dialog) {
//   var $content = $('<div><button class="btn btn-success">Revert button status right now.</button></div>');
//   var $footerButton = dialog.getButton('btn-1');
//   $content.find('button').click({
//     $footerButton: $footerButton
//   }, function(event) {
//     event.data.$footerButton.enable();
//     event.data.$footerButton.stopSpin();
//     dialog.setClosable(true);
//   });
//   return $content;
// },

// var dialog = new BootstrapDialog({
//   message: 'Hi Apple!',
//   buttons: [{
//     id: 'btn-1',
//     label: 'Button 1'
//   }]
// });
// dialog.realize();
// var btn1 = dialog.getButton('btn-1');
// btn1.click({
//   'name': 'Apple'
// }, function(event) {
//   alert('Hi, ' + event.data.name);
// });
// dialog.open();
